package io.itit.ecp.admin.provide.action.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.common.enums.IMeterDeviceEnums;
import io.itit.ecp.admin.provide.action.req.EmissionReductionDataQueryReq;
import io.itit.ecp.admin.provide.action.resp.EmissionIntensityDataResp;
import io.itit.ecp.admin.provide.action.resp.EmissionReductionDataResp;
import io.itit.ecp.admin.provide.helper.SceneHelper;
import io.itit.ecp.admin.server.entity.*;
import io.itit.ecp.admin.server.entity.extend.EnergyDataExtEntity;
import io.itit.ecp.admin.server.service.IEnergyDataService;
import io.itit.grass.common.provide.action.req.BaseBatchIdReq;
import io.itit.grass.common.provide.action.req.BaseIdReq;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IExport;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.common.provide.action.validation.IRequired;
import io.itit.grass.estelle.starter.core.ExcelXSSFUtil;
import io.itit.grass.estelle.starter.util.JsonUtil;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：碳排强度数据数据控制类
 * @Date：Create by 2023-11-23 16:28:52
 */
@Api(tags = "碳排强度数据")
@RestController
@RequestMapping("/ecp-admin/emissionIntensityData")
public class EmissionIntensityDataController {

    @Resource
    private IEnergyDataService energyDataService;

    @ApiOperation("分页查询")
    @PostMapping("queryPage")
    @RequiresPermissions("ecp-admin:emissionIntensityData:search")
    public ResultResp<IPage<EmissionIntensityDataResp>> queryPage(@RequestBody @Validated(IQueryPage.class) EmissionReductionDataQueryReq req) {
        IPage<EmissionIntensityDataResp> page = buildQW(req).joinPage(PageUtils.getPage(req, EmissionIntensityDataResp.class), EmissionIntensityDataResp.class);
        return ResultResp.success(SceneHelper.i().setSceneInfo(page));
    }

    /**
     * 组装查询条件
     */
    private static JoinLambdaWrapper<EnergyDataEntity> buildQW(EmissionReductionDataQueryReq req) {
        return Joins.of(EnergyDataEntity.class)
                .selectAll()
                .leftJoin(MeterDeviceEntity.class, MeterDeviceEntity::getId, EnergyDataEntity::getMeterDeviceId)
                .selectAs(t -> t.addFunAlias(MeterDeviceEntity::getDeviceCode, EnergyDataExtEntity::getDeviceCode)
                        .addFunAlias(MeterDeviceEntity::getDeviceName, EnergyDataExtEntity::getDeviceName)
                        .addFunAlias(MeterDeviceEntity::getReadMeterType, EnergyDataExtEntity::getReadMeterType)
                        .addFunAlias(MeterDeviceEntity::getReadMeterTag, EnergyDataExtEntity::getReadMeterTag)
                )
                .notIn(MeterDeviceEntity::getReadMeterType, IMeterDeviceEnums.ReadMeterType.getZeroCarbonList())
                .end()
                .leftJoin(SceneProjectEntity.class, SceneProjectEntity::getId, EnergyDataEntity::getSceneProjectId)
                .selectAs(build ->
                        build
                                .add("ecp_tt_scene_project.project_name", "project_name", false)
                                .add("ecp_tt_scene_project.floor_area", "project_floor_area", false)
                )
                .end()
                .leftJoin(SceneEdificeEntity.class, SceneEdificeEntity::getId, EnergyDataEntity::getSceneEdificeId)
                .selectAs(SceneEdificeEntity::getFloorArea, EmissionIntensityDataResp::getEdificeFloorArea)
                .end()
                .leftJoin(SceneFloorEntity.class, SceneFloorEntity::getId, EnergyDataEntity::getSceneFloorId)
                .selectAs(SceneFloorEntity::getFloorArea, EmissionIntensityDataResp::getFloorArea)
                .end()
                .leftJoin(SceneRoomEntity.class, SceneRoomEntity::getId, EnergyDataEntity::getSceneRoomId)
                .selectAs(SceneRoomEntity::getRoomArea, EmissionIntensityDataResp::getArea)
                .end()
                .eq(StringUtils.isNotEmpty(req.getSceneProjectId()), EnergyDataEntity::getSceneProjectId, req.getSceneProjectId())
                .eq(StringUtils.isNotEmpty(req.getSceneEdificeId()), EnergyDataEntity::getSceneEdificeId, req.getSceneEdificeId())
                .eq(StringUtils.isNotEmpty(req.getSceneFloorId()), EnergyDataEntity::getSceneFloorId, req.getSceneFloorId())
                .eq(StringUtils.isNotEmpty(req.getSceneRoomId()), EnergyDataEntity::getSceneRoomId, req.getSceneRoomId())
                .in(CollectionUtils.isNotEmpty(req.getIds()), EnergyDataEntity::getId, req.getIds())
                .ge(req.getBeginTime() != null, EnergyDataEntity::getSolidifyDate, req.getBeginTime())
                .le(req.getEndTime() != null, EnergyDataEntity::getSolidifyDate, req.getEndTime())
                .last(StringUtils.isNotEmpty(req.getSortColumn()), PageUtils.lastOrderBy(req))
                .orderByDesc(StringUtils.isEmpty(req.getSortColumn()), EnergyDataEntity::getSolidifyDate);
    }

    @ApiOperation("删除(根据Id删)")
    @PostMapping("deleteById")
    @RequiresPermissions("ecp-admin:emissionIntensityData:deleteById")
    public ResultResp<Boolean> deleteById(@RequestBody @Validated({IRequired.class}) BaseIdReq req) {
        return ResultResp.success(energyDataService.removeById(req.getId()));
    }

    @ApiOperation("批量删除")
    @PostMapping("batchDelete")
    @RequiresPermissions("ecp-admin:emissionIntensityData:batchDelete")
    public ResultResp<Boolean> batchDelete(@RequestBody @Validated({IRequired.class}) BaseBatchIdReq req) {
        return ResultResp.success(energyDataService.removeBatchByIds(req.getIds(), 1000));
    }

    @ApiOperation("导出")
    @PostMapping("export")
    @RequiresPermissions("ecp-admin:emissionIntensityData:export")
    public void export(@RequestBody @Validated(IExport.class) EmissionReductionDataQueryReq req, HttpServletResponse response) {
        List<EmissionReductionDataResp> list = SceneHelper.i().setSceneInfo(buildQW(req).joinList(EmissionReductionDataResp.class));
        ExcelXSSFUtil.export("EmissionReductionDataExport", JsonUtil.toJson(list), response);
    }

}
